<template>
  <div>
    <el-form ref="addFormRef" :rules="rules" :model="addForm.data" label-width="120">
      <el-form-item label="上级部门" prop="parentId">
        <el-tree-select
          v-model="addForm.data.parentId"
          :props="defaultProps"
          :data="deptTreeList.data"
          check-strictly
          :render-after-expand="false"
          style="width: 320px"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="addForm.data.name"
          placeholder="请输入部门名称"
          style="width: 320px"
        />
      </el-form-item>
      <el-form-item label="显示排序" prop="sort">
        <el-input-number
          v-model="addForm.data.sort"
          placeholder="请输入选择排序"
          style="width: 320px"
          :min="1"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(addFormRef)">重 置</el-button>
            <el-button class="confirm-btn" @click="submitForm(addFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { getDeptAdd } from "../request.js";
const emits = defineEmits(["operationDone"]);

const props = defineProps({
  operationId: {
    type: Object,
    default: () => {},
  },
  listData: {
    type: Object,
    default: () => {},
  },
});

const defaultProps = {
  children: "nodes",
  label: "name",
  value: "id",
};

const addFormRef = ref("");
const addForm = reactive({
  data: {
    sort:1,
    parentId: props.operationId.id,
  },
});

const rules = reactive({
  parentId: [{ required: true, message: "请选择上级部门", trigger: "change" }],
  name: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
  sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getDeptAdd(addForm.data).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = async (formEl) => {
  if (!formEl) return;
  addForm.data = {
    sort: 1,
  };
  formEl.resetFields();
};

const deptTreeList = reactive({
  data: [],
});

onMounted(() => {
  // setTimeout(() => {
    deptTreeList.data = props.listData.deptTreeList;
  // }, 1000);
});
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>
